package exp8;


import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;

class Window extends JFrame {
	Window(String title) {
		this.setTitle(title);
		this.setSize(1000, 500);
		this.setResizable(false);
		this.setLocationRelativeTo(null);
		this.setLayout(null);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}

	void showMe() {
		this.setVisible(true);
	}

}

public class Exp9_3 implements Runnable {
	Thread t = new Thread(this);
	Window w = new Window("弹砖游戏");
	JButton bt = new JButton();
	ImageIcon image1 = new ImageIcon("images/first.png");
	ImageIcon image2 = new ImageIcon("images/last.png");
	boolean b = true;
	int x;

	@Override
	public void run() {

		while (true) {
			x = bt.getX();
			if (x == 0) {
				b = false;
				bt.setIcon(image2);
			} else if (x == 950) {
				b = true;
				bt.setIcon(image1);
			}
			if (b) {
				x = x - 50;
				bt.setLocation(x, bt.getY());
			} else {
				x = x + 50;
				bt.setLocation(x, bt.getY());
			}
			try {
				Thread.sleep(500);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}

		}

	}

	void init() {
		bt.setSize(50, 50);
		bt.setLocation(450, 200);
		bt.setIcon(image1);
		w.add(bt);
		w.showMe();
		t.start();
	}

	public static void main(String[] args) {
		new Exp9_3().init();
	}
}
